#define _CRT_SECURE_NO_WARNINGS


class Solution {
public:
    int minOperations(vector<int>& nums, int x) {
        int left = 0;
        int right = 0;
        int size = nums.size();
        int sum = 0;
        for (int i = 0; i < size; i++)
        {
            sum += nums[i];
        }
        int target = sum - x;
        int len = -1;
        sum = 0;
        for (; right < size && left < size; right++)
        {
            sum += nums[right];

            if (sum >= target)
            {
                while (left<size && sum>target)
                {
                    sum -= nums[left++];
                }

                if (sum == target)
                {
                    len = max(len, right - left + 1);
                }
            }
        }
        if (len == -1)
            return -1;
        return size - len;
    }
};


class Solution {
public:
    bool check(string& s, int left, int right)
    {
        for (int i = left; i < right; i++)
        {
            if (s[i] == s[right])
                return true;
        }
        return false;
    }
    int lengthOfLongestSubstring(string s) {
        int right = 0;
        int left = 0;
        int size = s.size();
        int len = 0;
        for (; right < size; right++)
        {
            if (check(s, left, right))
            {
                len = max(len, right - left);
                while (1)
                {
                    if (s[left] == s[right])
                    {
                        left++;
                        break;
                    }
                    left++;
                }
            }
        }
        len = max(len, right - left);
        return len;
    }
};



class Solution {
public:
    int longestOnes(vector<int>& nums, int k) {
        int left = 0;
        int right = 0;
        int size = nums.size();
        int zero_count = 0;
        int len = 0;
        for (; right < size; right++)
        {
            if (nums[right] == 0)
                zero_count++;
            if (zero_count > k)
            {
                if (right - left > len)
                    len = right - left;
                zero_count = k;
                while (nums[left++] != 0)
                {
                    ;
                }
            }
        }
        if (right - left > len)
        {
            len = right - left;
        }
        return len;
    }
};